const express = require("express")
const mongoose = require("mongoose")
const cors = require("cors")
// 路由引入
const carouselRoutes = require("./routes/carousels")
const areaIconRoutes = require("./routes/areaicon")
const doctorRoutes = require("./routes/doctors")
const productRouter = require("./routes/product")
const articleRouter = require('./routes/article');
const iconRouter = require('./routes/icon');
const healthRoutes = require('./routes/health');

const app = express()

// 中间件
app.use(express.json())
app.use(cors())

// 连接 MongoDB
mongoose
  .connect("mongodb://localhost:27017/mydb", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
  })
  .then(() => console.log("连接上了数据库喵喵喵"))
  .catch((err) => console.error("Connection error:", err))

// 挂载路由
app.use("/api/carousels", carouselRoutes)
app.use("/api/products", productRouter)
app.use("/api/areaicon", areaIconRoutes)
app.use("/api/doctors", doctorRoutes)
app.use('/api/articles', articleRouter);
app.use('/api/health', healthRoutes);
app.use('/api/icons', iconRouter);

// 错误处理中间件
app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message })
})

const PORT = 3000
app.listen(PORT, () => {
  console.log(`服务器开启了喵喵喵~~~ http://localhost:${PORT}`)
})
